package com.ruoyi.web.controller.question;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.domain.question.Question;
import com.ruoyi.common.domain.question.vo.QuestionEditItemVM;
import com.ruoyi.common.domain.question.vo.QuestionEditRequestVM;
import com.ruoyi.common.enums.question.QuestionTypeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.question.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/api/question")
public class QuestionApiController {

    @Autowired
    private IQuestionService questionService;

    private RestTemplate restTemplate = new RestTemplate();

    @GetMapping("/get1000ByCd")
    public void get1000ByCd(String idArr, String cidArr) throws InterruptedException {
        String[] ids = idArr.split(",");
        String[] cids = cidArr.split(",");
        for (int m = 0; m < ids.length; m++) {
            String id = ids[m];
            String url = "http://zztk.cdky100.vip/apitk/zhangjie?tlevel_id=" + id + "&openid=oBSIe5ZfEMH81UV5LevFIAZVFI6g";
            ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, new HttpHeaders()), JSONObject.class);
            if(responseEntity == null || responseEntity.getBody() == null) {
                return;
            }
            JSONObject object = responseEntity.getBody();
            JSONArray ttList = object.getJSONArray("list");
            if(ttList == null || ttList.size() <= 0) {
                return;
            }
            int index = 1;
            for (int i = 0; i < ttList.size(); i++) {
                JSONObject tt = ttList.getJSONObject(i);
                System.out.println(tt);

                url = "http://zztk.cdky100.vip/apitk/detail?tlevel_id=" + tt.getString("tlevel_id") + "&openid=oBSIe5ZfEMH81UV5LevFIAZVFI6g";
                responseEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, new HttpHeaders()), JSONObject.class);
                if (responseEntity == null || responseEntity.getBody() == null) {
                    return ;
                }
                JSONObject resObj = responseEntity.getBody();
                resObj = resObj.getJSONObject("detail");
                JSONArray data = resObj.getJSONArray("timu");
                if (data == null || data.size() <= 0) {
                    return ;
                }
                String z = tt.getString("name");
                for (int k = 0; k < data.size(); k++) {
                    LinkedHashMap<String, Object> subData = (LinkedHashMap) data.get(k);
                    QuestionEditRequestVM question = new QuestionEditRequestVM();

                    question.setSubjectId(Integer.valueOf(cids[m]));
                    question.setGradeLevel(1);
                    question.setDifficult(0);
                    question.setOrderNum(index++);

                    String content = subData.get("title").toString();
                    question.setTitle(content);
//                    // 设置解析
                    String jx = subData.get("jiexi").toString();
                    if(StringUtils.isNotEmpty(jx)) {
                        jx = jx.replaceAll("&ldquo;", "“").replaceAll("&rdquo;", "”");
                        jx = jx.replaceAll("&hellip;", "...");
                    }
                    question.setAnalyze(jx);
                    question.setRemark(z);

                    // 解析正确选项
                    if (StringUtils.equals(subData.get("type").toString(), "多选")) {
                        question.setQuestionType(QuestionTypeEnum.MultipleChoice.getCode());
                        question.setScore("2");
                    } else {
                        question.setQuestionType(QuestionTypeEnum.SingleChoice.getCode());
                        question.setScore("1");
                    }
                    String right_text = subData.get("right_text").toString();
                    // 解析选项
                    List<QuestionEditItemVM> optionList = new ArrayList<>();
                    JSONObject options = JSON.parseObject(subData.get("xuanxiang").toString());

                    String[] op = new String[]{"A", "B", "C", "D"};
                    for (String o : op) {
                        QuestionEditItemVM option = new QuestionEditItemVM();
                        option.setContent(options.getString(o).replace(o + ".", ""));
                        option.setPrefix(o);
                        optionList.add(option);
                    }
                    question.setItems(optionList);

                    question.setCorrect(StringUtils.join(right_text.split(""), ","));
                    question.setCorrectArray(Arrays.asList(right_text.split("")));

                    Question res = questionService.insertFullQuestion(question,"admin");
                    System.out.println(JSON.toJSONString(res));
                }
            }
        }
    }

}
